package com.ziyi.tiantianshuiyin.util;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;
import com.gtdev5.geetolsdk.mylibrary.widget.PressedTextView;
import com.ziyi.tiantianshuiyin.BrowserUsActivity;
import com.ziyi.tiantianshuiyin.R;

/**
 * Created by ZL on 2019/10/12
 * <p>
 * 用户及隐私协议弹窗
 */

public class AgreementDialog extends Dialog {
    TextView mHintText;
    PressedTextView mCancelText;
    PressedTextView mAgreeText;

    private Context context;
    private onDialogClickListener mOnDialogClickListener;
    private boolean saveStatus = true; // 保存协议状态

    public onDialogClickListener getOnDialogClickListener() {
        return mOnDialogClickListener;
    }

    public void setOnDialogClickListener(onDialogClickListener mOnDialogClickListener) {
        this.mOnDialogClickListener = mOnDialogClickListener;
    }

    public AgreementDialog(Context context) {
        super(context, R.style.AgreementDialog);
        this.context = context;
    }

    public AgreementDialog(Context context, onDialogClickListener listener) {
        super(context, R.style.AgreementDialog);
        this.context = context;
        this.mOnDialogClickListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = View.inflate(context, R.layout.service_agreement, null);

        mHintText = view.findViewById(R.id.tv_hint);
        mCancelText = view.findViewById(R.id.tv_cancel);
        mAgreeText = view.findViewById(R.id.tv_agree);


        setCancelable(false);//点击外部不可dismiss
        setCanceledOnTouchOutside(false);
        Window window = this.getWindow();
        window.setContentView(view);
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        if (saveStatus) {
            initTextView();
        }

        mCancelText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SpUtils.getInstance().putBoolean(AppConfig.IS_CONFIRM_USER_AGREEMENT, false);
                if (mOnDialogClickListener != null) {
                    mOnDialogClickListener.onCancel();
                }
                dismiss();
            }
        });

        mAgreeText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (saveStatus) {
                    SpUtils.getInstance().putBoolean(AppConfig.IS_CONFIRM_USER_AGREEMENT, true);
                }
                if (mOnDialogClickListener != null) {
                    mOnDialogClickListener.onConfirm();
                }
                dismiss();
            }
        });
    }

    /**
     * 初始化webView
     */
    private void initTextView() {
        String name = context.getResources().getString(R.string.app_name);
        final SpannableStringBuilder style = new SpannableStringBuilder();
        String test = "亲爱的用户，欢迎使用本软件!为了保护您的隐私和个人信息，在您使用本软件前请认真阅读" +
                "《用户协议》以及《隐私政策》。我们将严格遵守您同意的各项条款，特向您说明如下:\n" +
                "1.为向您提供软件使用的基本功能，我们会收集、使用必要的信息;\n" +
                "2.基于您的明示授权，我们可能会获取您的定位权限(定位到您所在的当前位置)、存储权限(读取保存您的位置信息)、摄像头和拍照权限(意见反馈上传照片拍取照片)等信息,您有权拒绝或取消授权;\n" +
                "3.我们会采取世界先进的安全措施保护您的信息安全。";
        //设置文字
        int result1 = test.indexOf("《隐私政策》");
        int result2 = test.indexOf("《用户协议》");
        style.append(test);
        //隐私政策
        ClickableSpan PrivacyPolicy = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(context, BrowserUsActivity.class);
                intent.putExtra("URL", "file:///android_asset/conceal.html");
                intent.putExtra("name", "隐私政策");
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setUnderlineText(false);
            }
        };
        //用户协议
        ClickableSpan ServiceAgreement = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Intent intent = new Intent(context, BrowserUsActivity.class);
                intent.putExtra("URL", "file:///android_asset/useragreement.html");
                intent.putExtra("name", "用户协议");
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setUnderlineText(false);
            }
        };
        style.setSpan(PrivacyPolicy, result1, result1 + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(ServiceAgreement, result2, result2 + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mHintText.setText(style);
        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#ff6222"));
        ForegroundColorSpan foregroundColorSpan01 = new ForegroundColorSpan(Color.parseColor("#ff6222"));
        style.setSpan(foregroundColorSpan, result1, result1 + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(foregroundColorSpan01, result2, result2 + 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //配置给TextView
        mHintText.setMovementMethod(LinkMovementMethod.getInstance());
        //去掉点击后的背景颜色为透明
        mHintText.setHighlightColor(Color.parseColor("#00000000"));
        mHintText.setText(style);
    }

    /**
     * 设置按钮文字
     *
     * @param cancel  取消文字
     * @param confirm 同意文字
     * @param save    保存协议状态
     * @param hint    提示内容
     */
    public void setButtonText(String cancel, String confirm, boolean save, String hint) {
        mCancelText.setText(cancel);
        mAgreeText.setText(confirm);
        saveStatus = save;
        mHintText.setText(hint);
    }

    public interface onDialogClickListener {
        void onConfirm();

        void onCancel();
    }
}
